# -*- coding: UTF-8 -*-
# By Logic
"""
本文件用于服务器的Team模式下的右上方分数面板的改造
"""

import bs
import weakref

def bsScoreBoardinit(self, scoreboard, team, doCover, scale, label,
                    flashLength):

    self._scoreboard = weakref.ref(scoreboard)
    self._doCover = False
    self._scale = scale
    self._flashLength = flashLength
    self._width = 140.0 * self._scale
    self._height = 32.0 * self._scale
    self._barWidth = 2.0 * self._scale
    self._barHeight = 32.0 * self._scale
    self._barTex = self._backingTex = bs.getTexture('bar')
    self._coverTex = bs.getTexture('uiAtlas')
    self._model = bs.getModel('meterTransparent')

    safeTeamColor = bs.getSafeColor(team.color, targetIntensity=1.0)

    vr = bs.getEnvironment()['vrMode']

    if self._doCover:
        if vr:
            self._backingColor = [0.1 + c * 0.1 for c in safeTeamColor]
        else:
            self._backingColor = [0.05 + c * 0.17 for c in safeTeamColor]
    else:
        self._backingColor = [0.05 + c * 0.1 for c in safeTeamColor]

    self._backing = bs.NodeActor(
        bs.newNode('image',
                attrs={
                    'scale': (self._width, self._height),
                    'opacity':
                    (0.8 if vr else 0.8) if self._doCover else 0.5,
                    'color': self._backingColor,
                    'vrDepth': -3,
                    'attach': 'topLeft',
                    'texture': self._backingTex
                }))

    self._barColor = safeTeamColor
    self._bar = bs.NodeActor(
        bs.newNode('image',
                attrs={
                    'opacity': 0.7,
                    'color': self._barColor,
                    'attach': 'topLeft',
                    'texture': self._barTex
                }))

    self._barScale = bs.newNode('combine',
                                owner=self._bar.node,
                                attrs={
                                    'size': 2,
                                    'input0': self._barWidth,
                                    'input1': self._barHeight
                                })

    self._barScale.connectAttr('output', self._bar.node, 'scale')

    self._barPosition = bs.newNode('combine',
                                owner=self._bar.node,
                                attrs={
                                    'size': 2,
                                    'input0': 0,
                                    'input1': 0
                                })

    self._barPosition.connectAttr('output', self._bar.node, 'position')

    self._coverColor = safeTeamColor

    if self._doCover:
        self._cover = bs.NodeActor(
            bs.newNode('image',
                    attrs={
                        'scale': (self._width * 1.15, self._height * 1.6),
                        'opacity': 1.0,
                        'color': self._coverColor,
                        'vrDepth': 2,
                        'attach': 'topLeft',
                        'texture': self._coverTex,
                        'modelTransparent': self._model
                    }))

    c = safeTeamColor
    self._scoreText = bs.NodeActor(
        bs.newNode('text',
                attrs={
                    'hAttach': 'left',
                    'vAttach': 'top',
                    'hAlign': 'right',
                    'vAlign': 'center',
                    'maxWidth': 130.0 * (1.0 - scoreboard._scoreSplit),
                    'vrDepth': 2,
                    'scale': self._scale * 0.9,
                    'text': '',
                    'shadow': 1.0 if vr else 0.5,
                    'flatness':
                    (1.0 if vr else 0.5) if self._doCover else 1.0,
                    'color': c
                }))

    c = safeTeamColor

    if label is not None:
        teamNameLabel = label
    else:
        teamNameLabel = team.name

        # we do our own clipping here; should probably try to tap into some
        # existing functionality
        if type(teamNameLabel) is bs.Lstr:

            # hmmm; if the team-name is a non-translatable value lets go
            # ahead and clip it otherwise we leave it as-is so
            # translation can occur..
            if teamNameLabel.isFlatValue():
                v = teamNameLabel.evaluate()
                # in python < 3.5 some unicode chars can have length 2,
                # so we need to convert to raw int vals for safer trimming
                vChars = bs.uniToInts(v)
                if len(vChars) > 10:
                    teamNameLabel = bs.Lstr(value=bs.uniFromInts(vChars[:10]) +
                                            '...')
        else:
            # in python < 3.5 some unicode chars can have length 2,
            # so we need to convert to raw int vals for safe trimming
            teamNameLabelChars = bs.uniToInts(teamNameLabel)
            if len(teamNameLabelChars) > 10:
                teamNameLabel = bs.uniFromInts(teamNameLabelChars[:10]) + '...'
            teamNameLabel = bs.Lstr(value=teamNameLabel)

    self._nameText = bs.NodeActor(
        bs.newNode('text',
                attrs={
                    'hAttach': 'left',
                    'vAttach': 'top',
                    'hAlign': 'left',
                    'vAlign': 'center',
                    'vrDepth': 2,
                    'scale': self._scale * 0.9,
                    'shadow': 1.0 if vr else 0.5,
                    'flatness':
                    (1.0 if vr else 0.5) if self._doCover else 1.0,
                    'maxWidth': 130 * scoreboard._scoreSplit,
                    'text': teamNameLabel,
                    'color': c + (1.0, )
                }))

    self._score = None
